#include <View.h>


/********************************************/
/*              Members                     */
/********************************************/

    /****************************************/
    /*          Constructors                */
    /****************************************/

        View::View()
        {
            m_Projection = Matrix4::Perspective(70.0, 1.0, 0.001, 1000.0);
            m_ViewMatrix = new Matrix4();
            m_Position  =   new vec4(0.0, 0.0, -5.0);
            MainView = this;
            teta=0.0;
            phi=0.0;
            xAnchor=-1;
            yAnchor=-1;
            buttonPressed=false;
        }

    /****************************************/
    /*          Methods                     */
    /****************************************/

        void View::SendMatrix(GLuint programID)
        {
            glUniformMatrix4fv(glGetUniformLocation(programID, "u_ProjectionMatrix"),   1, GL_TRUE, m_Projection.array);
            glUniformMatrix4fv(glGetUniformLocation(programID, "u_ViewMatrix"),         1, GL_TRUE, m_ViewMatrix->array);
        }

    /****************************************/
    /*          Virtual Methods             */
    /****************************************/

        void View::Display()
        {

        }

        void View::Update()
        {
            float val = 0.1;

            /*if( sf::Keyboard::isKeyPressed(sf::Keyboard::A))
            {
                *m_Position += m_Forward * val;
            }
            if( sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
            {
               *m_Position -= m_Forward * val;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
            {
              m_Position->x +=val;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
               m_Position->x +=val;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                m_Position->x +=val;
            }
            if( sf::Keyboard::isKeyPressed(sf::Keyboard::X))
            {
                m_Position->x +=val;
            }

            if(Application::s_CurrentEvent.type == sf::Event::MouseButtonPressed)
            {
                    xAnchor=Application::s_CurrentEvent.mouseButton.x;
                    yAnchor=Application::s_CurrentEvent.mouseButton.y;
                    buttonPressed=true;
            }
            if(Application::s_CurrentEvent.type == sf::Event::MouseButtonReleased)
            {
                    buttonPressed=false;
                    xAnchor=-1;
                    yAnchor=-1;
            }
            if(Application::s_CurrentEvent.type == sf::Event::MouseMoved)
            {
                if(buttonPressed)
                {
                    int difX = xAnchor-Application::s_CurrentEvent.mouseMove.x;
                    int difY = yAnchor-Application::s_CurrentEvent.mouseMove.y;

                    RotateView(difX, difY);

                    xAnchor=Application::s_CurrentEvent.mouseMove.x;
                    yAnchor=Application::s_CurrentEvent.mouseMove.y;
                }
            }
*/
            m_ViewMatrix->Identity();
            *m_ViewMatrix  =    (Quaternion::Euler(-teta, -phi,0.0 )).RotationMatrix();
            m_ViewMatrix->Translate(m_Position->x, m_Position->y, m_Position->z);

            ComputeForward();

        }

        void View::ComputeForward()
        {
            Matrix4 rotationMat( (Quaternion::Euler(teta, phi,0.0 )).RotationMatrix());

            vec4    trolol(0.0,0.0,0.0,1.0);

            rotationMat.Translate(0.0, 0.0, 1.0);

            m_Forward  =   (((rotationMat))* (trolol));

            m_Forward   =   m_Forward.Normalize();
            m_Forward.Print();
        }

        void View::RotateView(int x, int y)
        {
            phi     +=  (float)x*0.005;
            teta    +=  (float)y*0.005;
        }

        void View::UpdateMatrix()
        {
            m_ViewMatrix->Identity();
        }

    /****************************************/
    /*          Operator Overload           */
    /****************************************/


    /****************************************/
    /*          Static                      */
    /****************************************/

        /****************************************/
        /*              Properties              */
        /****************************************/

            View *  View::MainView;

        /****************************************/
        /*          Methods                     */
        /****************************************/


